(function (vc) {
    var DEFAULT_PAGE = 1;
    var DEFAULT_ROWS = 100;
    vc.extends({
        data: {
            cartInfo: {
                prods: [],
                total: 0,
                records: 1,
                moreCondition: false,
                totalMount: 0.00,
                totalNum: 0,
                conditions: {
                    storeId: '',
                    name: '',
                    address: '',
                    tel: '',
                    createUserId: '',
                    state: ''
                }
            }
        },
        _initMethod: function () {
            vc.component._listOrderCarts(DEFAULT_PAGE, DEFAULT_ROWS);
            // $that._payFee();

        },
        _initEvent: function () {

        },
        methods: {
            _caculTotalMount: function () {
                let j = 0,k =0;
                vc.component.cartInfo.prods.forEach(item => {
                    if (!isNaN(item.cartNum) && !isNaN(item.price)) {
                        j += parseFloat(item.cartNum) * parseFloat(item.price).toFixed(2);
                        k += parseFloat(item.cartNum)
                    } else {
                        $that.cartInfo.totalMount = "请输入正确的数字";
                        $that.cartInfo.totalNum = "请输入正确的数字";
                        throw Error();
                    }
                });
                $that.cartInfo.totalMount = j.toFixed(2);
                $that.cartInfo.totalNum = k;
            },
            editProdcartNum: function (_prod,_newNum) {
                const reg = /^[1-9]\d*$|^0$/;
                if(!reg.test(_newNum)){
                    vc.message("请输入有效数字");
                    return;
                }
                if(_newNum == 0){
                    vc.message("请输入大于零的数字");
                    return;
                }
                _prod.cartNum = _newNum;
                vc.http.apiPost(
                    'storeCart.updateStoreCart',
                    JSON.stringify(_prod), {
                    emulateJSON: true
                },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            vc.component._listOrderCarts(DEFAULT_PAGE, DEFAULT_ROWS);
                            return;
                        }
                        vc.message(_json.msg);
                    },
                    function (errInfo, error) {
                        vc.message(json);

                    });
            },
            //增加购物车商品
            addProd: function (_prod) {
                _prod.cartNum = parseInt(_prod.cartNum) + 1;
                vc.http.apiPost(
                    'storeCart.updateStoreCart',
                    JSON.stringify(_prod), {
                    emulateJSON: true
                },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            vc.component._listOrderCarts(DEFAULT_PAGE, DEFAULT_ROWS);
                            return;
                        }
                        vc.message(_json.msg);
                    },
                    function (errInfo, error) {
                        vc.message(json);

                    });
            },
            //减少购物车商品
            minusProd: function (_prod) {
                if(_prod.cartNum <=1){
                    vc.message("不能在减少了，最小数量为1");
                    return;
                }
                _prod.cartNum = _prod.cartNum - 1;
                vc.http.apiPost(
                    'storeCart.updateStoreCart',
                    JSON.stringify(_prod), {
                    emulateJSON: true
                },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            vc.component._listOrderCarts(DEFAULT_PAGE, DEFAULT_ROWS);
                            return;
                        }
                        vc.message(_json.msg);
                    },
                    function (errInfo, error) {
                        vc.message(json);

                    });
            },

            //删除购物车商品
            delProd: function (_prod) {
                vc.http.apiPost(
                    'storeCart.deleteStoreCart',
                    JSON.stringify(_prod), {
                    emulateJSON: true
                },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            vc.component._listOrderCarts(DEFAULT_PAGE, DEFAULT_ROWS);
                            return;
                        }
                        vc.message(_json.msg);
                    },
                    function (errInfo, error) {
                        vc.message(json);

                    });
            },
            //获取购物车订单
            _listOrderCarts: function (_page, _rows) {
                vc.component.cartInfo.conditions.page = _page;
                vc.component.cartInfo.conditions.row = _rows;
                var param = {
                    params: vc.component.cartInfo.conditions
                };
                //发送get请求
                vc.http.apiGet('storeCart.listStoreCart',
                    param,
                    function (json, res) {
                        var _cartInfo = JSON.parse(json);
                        vc.component.cartInfo.total = _cartInfo.total;
                        vc.component.cartInfo.records = _cartInfo.records;
                        vc.component.cartInfo.prods = _cartInfo.data;
                        //vc.component._caculTotalMount();
                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            gotOrder:function () {
                let _payGoods = [];
                vc.component.cartInfo.prods.forEach(item => {
                    if (item.checked == true) {
                        _payGoods.push(item);
                    }
                });
                if (_payGoods.length < 1) {
                    vc.message("请先选择商品");
                    return;
                }
                let _shopId = _payGoods[0].shopId;
                let _oneShopGoods = true;
                _payGoods.forEach(_g => {
                    if (_shopId != _g.shopId) {
                        _oneShopGoods = false;
                    }
                });
                if (!_oneShopGoods) {
                    vc.message("必须同店铺一起支付");
                    return;
                }
                let _productId= '';
                let _valueId = '';
                let _goodsNum = '';
                let _cartId = '';
                _payGoods.forEach(_g => {
                    _productId +=(_g.productId+',');
                    _valueId += (_g.valueId+',');
                    _goodsNum += (_g.cartNum+',');
                    _cartId += (_g.cartId+',');
                });

                //无商品提示
                if(_payGoods.length == 0){
                    vc.message("请先选择商品");
                    return;
                }
                vc.jumpToPage('/pageLR.html#/pages/plateform/order?productId='+_productId+'&shopId='+_shopId+'&valueId='+_valueId+'&goodsNum='+_goodsNum+'&cartId='+_cartId);
            },
            onSel: function(_index, _prod) {
                $that.cartInfo.prods[_index].checked = $that.cartInfo.prods[_index].checked ? false : true;
                let totalNum=0, totalMount=0.0;
                $that.cartInfo.prods.forEach(item => {
                    if(item.checked == true) {
                        totalMount += parseFloat(item.cartNum) * parseFloat(item.price).toFixed(2);
                        totalNum += parseFloat(item.cartNum)
                    }
                });
                $that.cartInfo.totalMount = totalMount.toFixed(2);
                $that.cartInfo.totalNum = totalNum;

            },
        }
    });

})(window.vc);
